package com.haotunet.app.goldtreasure.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;

import java.util.Timer;
import java.util.TimerTask;

public class LauncherActivity extends Activity {
    private boolean isFirst;
    private boolean isSkip;
    private SharedPreferences sp;
    private ImageView mImage;
    private ImageView mAdImage;
    private Button btnSkip;
    private RelativeLayout adLayout;
    private ShareStore shareStore;
    private boolean isClickAd;
    Handler handler = new Handler();
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:
                    mImage.setVisibility(View.GONE);
                    if (!shareStore.getString("start_img", "").equals("")) {
                        btnSkip.setVisibility(View.VISIBLE);
                        adLayout.setVisibility(View.VISIBLE);
                        mAdImage.setEnabled(true);
                        if (isFinishing()) {
                            return;
                        }
                        Glide.with(LauncherActivity.this).load(shareStore.getString("start_img", "").trim()).into(mAdImage);
                    }
                    break;
                case 1:
                    Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
                    intent.putExtra("type", "");
                    startActivity(intent);
                    overridePendingTransition(R.anim.fade, R.anim.hold);
                    finish();
                    break;
                default:

                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//
        mImage = (ImageView) findViewById(R.id.launch_image);
        mAdImage = (ImageView) findViewById(R.id.ad_image);
        btnSkip = (Button) findViewById(R.id.btn_skip);
        adLayout = (RelativeLayout) findViewById(R.id.ad_layout);
        mAdImage.setEnabled(false);
        sp = getSharedPreferences(Strs.SYS_SET_FIRSTPAGE, MODE_PRIVATE);
        shareStore = new ShareStore(LauncherActivity.this);
        isFirst = sp.getBoolean(Strs.IS_FIRST_LOGIN, true);
        loadAd();
        mAdImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                isClickAd = true;
                Intent intentIndex = new Intent(LauncherActivity.this, MainActivity.class);
                intentIndex.putExtra("type", "");
                Intent intentInAd = new Intent(LauncherActivity.this, DynamicDetailActivity.class);
                intentInAd.putExtra("activityUrl", shareStore.getString("start_img_url", ""));
                intentInAd.putExtra("type", 1);
                Intent[] intents = new Intent[2];
                intents[0] = intentIndex;
                intents[1] = intentInAd;
                startActivities(intents);
                finish();
            }
        });
        btnSkip.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                isSkip = true;
                toMain();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                if (!isClickAd) {
                    if (!isSkip) {
                        switchToSkip();
                    }
                }
            }
        }, 2000);

    }

    private void loadAd() {
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                handle.sendEmptyMessage(0);
            }
        }, 500);
    }

    private void switchToSkip() {
        if (isFirst) {
            delayToSplash();
            Editor edit = sp.edit();
            edit.putBoolean(Strs.IS_FIRST_LOGIN, false);
            edit.apply();
        } else {
            delayToMain();
        }
    }

    private void delayToSplash() {

        toSplash();

    }

    private void toSplash() {
        Intent intent = new Intent(LauncherActivity.this, SplashActivity.class);
        startActivity(intent);
        finish();
    }

    private void delayToMain() {

        toMain();

    }

    protected void toMain() {
        handle.sendEmptyMessage(1);
    }

}
